<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\HouseFormat;
use App\Http\Requests\CreateHouserProfileAndHouseRequest;
use App\Services\HouserService;
use App\Services\HouseService;
use Illuminate\Http\Request;

class HouserController extends Controller
{
    protected $houserService;

    protected $houseService;

    public function __construct(HouserService $houserService, HouseService $houseService)
    {
        $this->houserService = $houserService;
        $this->houseService = $houseService;
    }

    public function createHouse(CreateHouserProfileAndHouseRequest $createHouserProfileAndHouseRequest)
    {
        // 创建房子信息
        // 创建房子和身份证绑定关系
        $houses = $createHouserProfileAndHouseRequest->input('houses') ?? [];
        $setHouseIds = array_column($houses, 'id');
        $houserId = $createHouserProfileAndHouseRequest->input('user_id');
        $myHouses = $this->houseService->getMyHouses($houserId);
        $myHouseIds = array_column($myHouses, 'id');
        $needDeleteHouses = array_values(array_diff($myHouseIds, $setHouseIds));
        if (! empty($needDeleteHouses)) {
            foreach ($needDeleteHouses as $deleteHouseId) {
                $this->houseService->deleteHouse($deleteHouseId);
            }
        }

        foreach ($houses as $house) {
            $houseFormat = new HouseFormat($house);
            if ($houseFormat->getId()) {
                $houseModel = $this->houseService->updateHouse($houseFormat);
            } else {
                $houseModel = $this->houseService->createHouse($houseFormat);
            }
            $tenants = $houseFormat->getTenants();
            if (! empty($tenants)) {
                $tenantsIdCardNums = array_column($tenants, 'id_card_number');
                $settedTenantsIdCardNums = $this->houseService->getHouseTenants($houseModel->id)->pluck('id_card_number')->toArray();
                $needDeleteTenants = array_values(array_diff($settedTenantsIdCardNums, $tenantsIdCardNums));
                if (! empty($needDeleteTenants)) {
                    $this->houseService->deleteHouseTenants($houseModel->id, $needDeleteTenants);
                }
                foreach ($tenants as $tenant) {
                    $this->houseService->setHouseIdCardBind($houseModel->id, $tenant['id_card_number'], $tenant['name']);
                }
            }
        }
        return renderSuccessJson();
    }

    public function getHouse(Request $request)
    {
        $houseId = $request->input('house_id');
        $res = $this->houseService->getHouseInfo($houseId);
        return renderSuccessJson($res);
    }

    public function houserSort(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $orderType = $request->input('order_type', 'desc');
        $name = $request->input('name') ?? '';
        $res = $this->houserService->houserSort($page, $pageSize, $orderType, $name);
        return renderSuccessJson($res);
    }

    public function getMyHouses(Request $request)
    {
        $userId = $request->input('user_id');
        $res = $this->houseService->getMyHouses($userId);
        return renderSuccessJson($res);
    }

    // 房东获取最终分数
    public function getMyFinalScore(Request $request)
    {
        $userId = $request->input('user_id');
        if (empty($userId)) {
            throw new ApiException('缺少参数', 1);
        }
        $res = $this->houseService->getMyFinalScore($userId);
        return renderSuccessJson($res);
    }

    // 审核房东的房屋信息 // 不用了
    public function checkHouserHouse(Request $request)
    {
        $houserUserId = $request->input('houser_user_id');
        if (empty($houserUserId)) {
            throw new ApiException('缺少参数', 1);
        }
        $checkStatus = $request->input('check_status');
        if (empty($checkStatus)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->houseService->checkHouserHouse($houserUserId, $checkStatus);
        return renderSuccessJson();
    }

    // 审核房屋
    public function checkHouse(Request $request)
    {
        $checkStatus = $request->input('check_status');
        if (empty($checkStatus)) {
            throw new ApiException('缺少参数', 1);
        }
        $houseId = $request->input('house_id');
        if (empty($houseId)) {
            throw new ApiException('缺少参数', 1);
        }
        $checkMessage = $request->input('check_message') ?? '';
        $this->houseService->checkHouse($houseId, $checkStatus, $checkMessage);
        return renderSuccessJson();
    }

    public function deleteHouser(Request $request)
    {
        $houserId = $request->input('houser_id');
        $this->houserService->deleteHouser($houserId);
        return renderSuccessJson();
    }
}
